package com.prolestar.partyConstruction.service.impl;

import com.prolestar.common.core.domain.AjaxResult;
import com.prolestar.common.utils.DateUtils;
import com.prolestar.common.utils.SecurityUtils;
import com.prolestar.partyConstruction.domain.PsCompanyPartyinfo;
import com.prolestar.partyConstruction.domain.PsCompanypartyActivites;
import com.prolestar.partyConstruction.mapper.PartyConstructionMapper;
import com.prolestar.partyConstruction.service.IPartyConstructionService;
import com.prolestar.system.service.ISysRoleService;
import com.prolestar.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class PartyConstructionServiceImpl implements IPartyConstructionService {
    @Autowired
    private PartyConstructionMapper partyConstructionMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService sysUserService;



    /**
     * 查询党建列表
     * @param psCompanyPartyinfo
     * @return
     */
    @Override
    public List<PsCompanyPartyinfo> selectpartyConstructionList(PsCompanyPartyinfo psCompanyPartyinfo){
        Long userId = SecurityUtils.getUserId();
        Set<String> permission = sysRoleService.selectRolePermissionByUserId(userId);
        psCompanyPartyinfo.setCreateBy(userId.toString());
        if (permission.contains("admin")||permission.contains("conservator")){
            List<PsCompanyPartyinfo> psCompanyPartyinfos = partyConstructionMapper.selectpartyConstructionList(psCompanyPartyinfo);
            psCompanyPartyinfos.forEach(p->{
                List<PsCompanypartyActivites> psCompanypartyActivitesList = partyConstructionMapper.selectPsCompanypartyActivitesById(p.getId());
                if (p.getCreateBy() != null && p.getCreateBy() != ""){
                    p.setCreateBy(sysUserService.selectUserById(Long.parseLong(p.getCreateBy())).getNickName());
                }
                p.setPsCompanypartyActivites(psCompanypartyActivitesList);
            });
            return psCompanyPartyinfos;
        }else {
            List<PsCompanyPartyinfo> psCompanyPartyinfos = partyConstructionMapper.selectpartyConstructionUserList(psCompanyPartyinfo);
            psCompanyPartyinfos.forEach(p->{
                List<PsCompanypartyActivites> psCompanypartyActivitesList = partyConstructionMapper.selectPsCompanypartyActivitesById(p.getId());
                if (p.getCreateBy() != null && p.getCreateBy() != ""){
                    p.setCreateBy(sysUserService.selectUserById(Long.parseLong(p.getCreateBy())).getNickName());
                }
                p.setPsCompanypartyActivites(psCompanypartyActivitesList);
            });
            return psCompanyPartyinfos;
        }
    }

    /**
     * 查询党建详情列表
     * @param id
     * @return
     */
    @Override
    public PsCompanyPartyinfo selectPartyConstructionById(Long id) {
        PsCompanyPartyinfo psCompanyPartyinfo =  partyConstructionMapper.selectPartyConstructionById(id);
        List<PsCompanypartyActivites> psCompanypartyActivites = partyConstructionMapper.selectPsCompanypartyActivitesById(id);
        psCompanyPartyinfo.setPsCompanypartyActivites(psCompanypartyActivites);
        return psCompanyPartyinfo;
    }

    /**
     * 新建党建
     * @param psCompanyPartyinfo
     * @return
     */
    @Override
    public void insertPsCompanyPartyinfo(PsCompanyPartyinfo psCompanyPartyinfo) {
        psCompanyPartyinfo.setIsDelete(0L);
        psCompanyPartyinfo.setCreateBy(SecurityUtils.getUserId().toString());
        psCompanyPartyinfo.setCreateTime(DateUtils.getNowDate());
        partyConstructionMapper.insertPsCompanyPartyinfo(psCompanyPartyinfo);
//        List<PsCompanypartyActivites> list = psCompanyPartyinfo.getPsCompanypartyActivites();
//        list.forEach(l->{
//            l.setCopartyId(psCompanyPartyinfo.getId());
//            l.setCreateBy(SecurityUtils.getUserId());
//            l.setCreateTime(DateUtils.getNowDate());
//            l.setIsDelete(0);
//            partyConstructionMapper.insertPsCompanypartyActivites(l);
//        });
    }



    /**
     * 修改党建
     * @param psCompanyPartyinfo
     * @return
     */
    @Override
    public AjaxResult updatePsCompanyPartyinfo(PsCompanyPartyinfo psCompanyPartyinfo) {
        psCompanyPartyinfo.setUpdateBy(SecurityUtils.getUserId().toString());
        psCompanyPartyinfo.setUpdateTime(DateUtils.getNowDate());
//        List<PsCompanypartyActivites> list = psCompanyPartyinfo.getPsCompanypartyActivites();
        partyConstructionMapper.updatePsCompanyPartyinfo(psCompanyPartyinfo);
//        list.forEach(l->{
//            partyConstructionMapper.updatePsCompanypartyActivites(l);
//
//        });
        return AjaxResult.success();
    }

    /**
     * 删除党建
     * @param ids
     * @return
     */
    @Override
    public AjaxResult deletePsCompanyPartyinfo(Long[] ids) {
        for (Long id: ids) {
            partyConstructionMapper.deletePsCompanyPartyinfo(id);
            partyConstructionMapper.deletePsCompanypartyActivites(id);
        }
        return AjaxResult.success();
    }

    /**
     * 新增三会一课
     * @param psCompanypartyActivites
     * @return
     */
    @Override
    public AjaxResult insertpsCompanypartyActivites(PsCompanypartyActivites psCompanypartyActivites) {
        psCompanypartyActivites.setIsDelete(0);
        psCompanypartyActivites.setCreateBy(SecurityUtils.getUserId());
        psCompanypartyActivites.setCreateTime(DateUtils.getNowDate());
        partyConstructionMapper.insertPsCompanypartyActivites(psCompanypartyActivites);
        return AjaxResult.success();

    }

    /**
     * 修改三会一课
     * @param psCompanypartyActivites
     * @return
     */
    @Override
    public AjaxResult updatepsCompanypartyActivites(PsCompanypartyActivites psCompanypartyActivites) {
        partyConstructionMapper.updatePsCompanypartyActivites(psCompanypartyActivites);
        return AjaxResult.success();
    }

    /**
     * 三会一课详情
     * @param id
     * @return
     */
    @Override
    public PsCompanypartyActivites getPcaInfo(Long id) {
        return partyConstructionMapper.getPcaInfo(id);
    }

    @Override
    public AjaxResult removePca(Long[] ids) {
        for (Long id: ids) {
            partyConstructionMapper.removePca(id);
        }
        return AjaxResult.success();
    }
}
